package cn.xspace.app.filemg.view;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.imcommon.utils.AppUtil;
import com.xspace.android.xafcommon.util.BaseRProxy;


import java.util.List;

import cn.xspace.app.filemg.bean.FileInfo;
import cn.xspace.app.filemg.bean.ImgPackInfo;
import cn.xspace.app.filemg.util.CallBack;
import cn.xspace.app.filemg.util.FileMg;

public class FileAppAdapter extends BaseExpandableListAdapter {
	Context context;
	List<ImgPackInfo> parentlist;
    private CallBack callBack;
	public FileAppAdapter(Context context,List<ImgPackInfo> parentlist,CallBack callBack) {
//		super();
		this.context = context;
		this.parentlist = parentlist;
		this.callBack=callBack;
	}

	@Override
	public Object getChild(int arg0, int arg1) {
		return parentlist.get(arg0).getImgList().get(arg1);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return arg1;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(BaseRProxy.file_app_child_item, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final FileInfo appInfo=parentlist.get(groupPosition).getImgList().get(childPosition);
		viewHolder.title.setText(appInfo.getName());
		viewHolder.appSize.setText(FileMg.FormetFileSize(appInfo.getSize()));
		viewHolder.appTime.setText(appInfo.getTime());
		//LogUtil.v("appFrag", "grouppos:"+groupPosition+" "+appInfo.getName()+" appInfo.getApkIoc():"+appInfo.getApkIoc()
		//		+" appInfo.getApkIocByte():"+appInfo.getApkIocByte());
		if (appInfo.getApkIocByte() != null) {
			viewHolder.image.setImageDrawable(AppUtil.convertBitmap2Drawable(AppUtil.createImage(appInfo
					.getApkIocByte())));
		} else {
			viewHolder.image.setImageDrawable(appInfo.getApkIoc());
		}

		viewHolder.mCheckBox.setChecked(appInfo.isCheckFlag());
	    convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				callBack.checkedInfo(appInfo);
			}
		});
		return convertView;
	}
	class ViewHolder {
		public TextView title;
		public ImageView image;
		public CheckBox mCheckBox;
		public TextView appSize;
		public TextView appTime;
		public ViewHolder(View v){
			title = (TextView) v.findViewById(BaseRProxy.ItemText);
			mCheckBox = (CheckBox) v.findViewById(BaseRProxy.box);
			image = (ImageView) v.findViewById(BaseRProxy.ItemImage);
			appSize = (TextView) v.findViewById(BaseRProxy.ItemText_size);
			appTime = (TextView) v.findViewById(BaseRProxy.ItemText_time);
		}
	}
	@Override
	public int getChildrenCount(int arg0) {
		return parentlist.get(arg0).getImgList().size();
	}

	@Override
	public Object getGroup(int arg0) {
		return parentlist.get(arg0);
	}

	@Override
	public int getGroupCount() {
		return parentlist.size();
	}

	@Override
	public long getGroupId(int arg0) {
		return arg0;
	}


	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
			ViewGroup parent) {
		ViewHolderP viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					BaseRProxy.manager_favoritor_web_item, null);
			viewHolder = new ViewHolderP(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolderP) convertView.getTag();
		}
		ImgPackInfo appInfo=parentlist.get(groupPosition);
		viewHolder.tv_collect.setVisibility(View.GONE);
		viewHolder.tv_add.setVisibility(View.GONE);
		viewHolder.tv_more.setVisibility(View.GONE);
		viewHolder.tv_title.setText(appInfo.getPackName());
		if(isExpanded){
			viewHolder.img.setImageResource(BaseRProxy.expander_ic_minimized);
		}
		else{
			viewHolder.img.setImageResource(BaseRProxy.expander_ic_maximized);
		}
		return convertView;
	}
	
	class ViewHolderP {
		public TextView tv_collect;
		public ImageView img;
		public TextView tv_more;
		public TextView tv_add;
		public TextView tv_title;
		public ViewHolderP(View arg2){
			 img = (ImageView) arg2
					.findViewById(BaseRProxy.manager_favorite_title_ico);
			 tv_collect = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_collection_item);
			 tv_add = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_new_folder);
			 tv_title = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_title_name);
			 tv_more = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_more);
		}
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		System.out.println("----------------------super.areAllItemsEnabled():"+super.areAllItemsEnabled());
		return super.areAllItemsEnabled();
	}
	

}
